<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="所属仓库">
          <j-select-warehouse-modal
            v-decorator="['warehouseId', validatorRules.warehouseId]"
            :disabled="!!warehouseIdFromParent"
            placeholder="请选择仓库"
            @change="handleWarehouseChange"
          />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="库位编号前缀">
          <a-input placeholder="请输入库位编号前缀" v-decorator="['codePrefix', validatorRules.codePrefix]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="库位名称前缀">
          <a-input placeholder="请输入库位名称前缀" v-decorator="['namePrefix', validatorRules.namePrefix]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="起始编号">
          <a-input-number placeholder="请输入起始编号" v-decorator="['startNum', validatorRules.startNum]" :min="1" :step="1" style="width: 100%" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="生成数量">
          <a-input-number placeholder="请输入生成数量" v-decorator="['count', validatorRules.count]" :min="1" :max="100" :step="1" style="width: 100%" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="编号位数">
          <a-input-number placeholder="请输入编号位数" v-decorator="['numLength', validatorRules.numLength]" :min="1" :max="10" :step="1" style="width: 100%" />
          <span class="ant-form-text">如：位数为3，则编号为001、002...</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="状态">
          <j-dict-select-tag type="list" v-decorator="['status', validatorRules.status]" dictCode="wms_location_status" placeholder="请选择状态"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-textarea placeholder="请输入备注" v-decorator="['remark']" :rows="4" />
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import { warehouseLocationBatchGenerate } from '@/api/wms'
  import JSelectWarehouseModal from '@/components/jeecgbiz/JSelectWarehouseModal.vue'
  
  export default {
    name: 'WarehouseLocationBatchModal',
    components: {
      JDictSelectTag,
      JSelectWarehouseModal
    },
    data() {
      return {
        title: '批量生成库位',
        visible: false,
        warehouseIdFromParent: '', // 从父组件传入的仓库ID
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          warehouseId: {
            rules: [
              { required: true, message: '请选择仓库!' }
            ]
          },
          codePrefix: {
            rules: [
              { required: true, message: '请输入库位编号前缀!' }
            ]
          },
          namePrefix: {
            rules: [
              { required: true, message: '请输入库位名称前缀!' }
            ]
          },
          startNum: {
            rules: [
              { required: true, message: '请输入起始编号!' }
            ]
          },
          count: {
            rules: [
              { required: true, message: '请输入生成数量!' }
            ]
          },
          numLength: {
            rules: [
              { required: true, message: '请输入编号位数!' }
            ]
          },
          status: {
            rules: [
              { required: true, message: '请选择状态!' }
            ]
          }
        }
      }
    },
    created() {
    },
    methods: {
      handleWarehouseChange(value, selectedWarehouses) {
        // 可以在这里添加仓库变更后的逻辑
        console.log('选择的仓库ID:', value)
      },
      show(warehouseId) {
        this.warehouseIdFromParent = warehouseId;
        this.form.resetFields();
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue({
            warehouseId: warehouseId || undefined,
            codePrefix: 'LOC',
            namePrefix: '库位',
            startNum: 1,
            count: 10,
            numLength: 3,
            status: '0'
          })
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            // 优先使用表单中选择的仓库ID，如果没有选择则使用父组件传入的
            warehouseLocationBatchGenerate(values).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close()
      }
    }
  }
</script> 